
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>xml.sax.saxutils --- SAX 工具集 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="xml.sax.xmlreader --- Interface for XML parsers" href="xml.sax.reader.html" />
    <link rel="prev" title="xml.sax.handler --- Base classes for SAX handlers" href="xml.sax.handler.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/xml.sax.utils.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="xml.sax.reader.html" title="xml.sax.xmlreader --- Interface for XML parsers"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="xml.sax.handler.html" title="xml.sax.handler --- Base classes for SAX handlers"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="markup.html" accesskey="U">结构化标记处理工具</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-xml.sax.saxutils">
<span id="xml-sax-saxutils-sax-utilities"></span><h1><a class="reference internal" href="#module-xml.sax.saxutils" title="xml.sax.saxutils: Convenience functions and classes for use with SAX."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax.saxutils</span></code></a> --- SAX 工具集<a class="headerlink" href="#module-xml.sax.saxutils" title="永久链接至标题">¶</a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/xml/sax/saxutils.py">Lib/xml/sax/saxutils.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#module-xml.sax.saxutils" title="xml.sax.saxutils: Convenience functions and classes for use with SAX."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax.saxutils</span></code></a> 模块包含一些在创建 SAX 应用程序时十分有用的类和函数，它们可以被直接使用，或者是作为基类使用。</p>
<dl class="function">
<dt id="xml.sax.saxutils.escape">
<code class="sig-prename descclassname">xml.sax.saxutils.</code><code class="sig-name descname">escape</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">entities={}</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.sax.saxutils.escape" title="永久链接至目标">¶</a></dt>
<dd><p>对数据字符串中对 <code class="docutils literal notranslate"><span class="pre">'&amp;'</span></code>, <code class="docutils literal notranslate"><span class="pre">'&lt;'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'&gt;'</span></code> 进行转义。</p>
<p>你可以通过传入一个字典作为可选的 <em>entities</em> 形参来对其他字符串数据进行转义。 字典的键和值必须为字符串；每个键将被替换为其所对应的值。 字符 <code class="docutils literal notranslate"><span class="pre">'&amp;'</span></code>, <code class="docutils literal notranslate"><span class="pre">'&lt;'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'&gt;'</span></code> 总是会被转义，即使提供了 <em>entities</em>。</p>
</dd></dl>

<dl class="function">
<dt id="xml.sax.saxutils.unescape">
<code class="sig-prename descclassname">xml.sax.saxutils.</code><code class="sig-name descname">unescape</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">entities={}</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.sax.saxutils.unescape" title="永久链接至目标">¶</a></dt>
<dd><p>对字符串数据中的 <code class="docutils literal notranslate"><span class="pre">'&amp;amp;'</span></code>, <code class="docutils literal notranslate"><span class="pre">'&amp;lt;'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'&amp;gt;'</span></code> 进行反转义。</p>
<p>你可以通过传入一个字典作为可选的 <em>entities</em> 形参来对其他数据字符串进行转义。 字典的键和值必须都为字符串；每个键将被替换为所对应的值。 <code class="docutils literal notranslate"><span class="pre">'&amp;amp'</span></code>, <code class="docutils literal notranslate"><span class="pre">'&amp;lt;'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'&amp;gt;'</span></code> 将总是保持不被转义，即使提供了 <em>entities</em>。</p>
</dd></dl>

<dl class="function">
<dt id="xml.sax.saxutils.quoteattr">
<code class="sig-prename descclassname">xml.sax.saxutils.</code><code class="sig-name descname">quoteattr</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">entities={}</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.sax.saxutils.quoteattr" title="永久链接至目标">¶</a></dt>
<dd><p>类似于 <a class="reference internal" href="#xml.sax.saxutils.escape" title="xml.sax.saxutils.escape"><code class="xref py py-func docutils literal notranslate"><span class="pre">escape()</span></code></a>，但还会对 <em>data</em> 进行处理以将其用作属性值。 返回值是 <em>data</em> 加上任何额外要求的替换的带引号版本。<a class="reference internal" href="#xml.sax.saxutils.quoteattr" title="xml.sax.saxutils.quoteattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">quoteattr()</span></code></a> 将基于 <em>data</em> 的内容选择一个引号字符，以尽量避免在字符串中编码任何引号字符。 如果单双引号字符在 <em>data</em> 中都存在，则双引号字符将被编码并且 <em>data</em> 将使用双引号来标记。 结果字符串可被直接用作属性值:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;&lt;element attr=</span><span class="si">%s</span><span class="s2">&gt;&quot;</span> <span class="o">%</span> <span class="n">quoteattr</span><span class="p">(</span><span class="s2">&quot;ab &#39; cd </span><span class="se">\&quot;</span><span class="s2"> ef&quot;</span><span class="p">))</span>
<span class="go">&lt;element attr=&quot;ab &#39; cd &amp;quot; ef&quot;&gt;</span>
</pre></div>
</div>
<p>此函数适用于为 HTML 或任何使用引用实体语法的 SGML 生成属性值。</p>
</dd></dl>

<dl class="class">
<dt id="xml.sax.saxutils.XMLGenerator">
<em class="property">class </em><code class="sig-prename descclassname">xml.sax.saxutils.</code><code class="sig-name descname">XMLGenerator</code><span class="sig-paren">(</span><em class="sig-param">out=None</em>, <em class="sig-param">encoding='iso-8859-1'</em>, <em class="sig-param">short_empty_elements=False</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.sax.saxutils.XMLGenerator" title="永久链接至目标">¶</a></dt>
<dd><p>这个类通过将 SAX 事件写回到 XML 文档来实现 <a class="reference internal" href="xml.sax.handler.html#xml.sax.handler.ContentHandler" title="xml.sax.handler.ContentHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ContentHandler</span></code></a> 接口。 换句话说，使用 <a class="reference internal" href="#xml.sax.saxutils.XMLGenerator" title="xml.sax.saxutils.XMLGenerator"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLGenerator</span></code></a> 作为内容处理程序将重新产生所解析的原始文档。 <em>out</em> 应当为一个文件类对象，它默认将为 <em>sys.stdout</em>。 <em>encoding</em> 为输出流的编码格式，它默认将为 <code class="docutils literal notranslate"><span class="pre">'iso-8859-1'</span></code>。 <em>short_empty_elements</em> 控制不包含内容的元素的格式化：如为 <code class="docutils literal notranslate"><span class="pre">False</span></code> (默认值) 则它们会以开始/结束标记对的形式被发送，如果设为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 则它们会以单个自结束标记的形式被发送。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能: </span><em>short_empty_elements</em> 形参。</p>
</div>
</dd></dl>

<dl class="class">
<dt id="xml.sax.saxutils.XMLFilterBase">
<em class="property">class </em><code class="sig-prename descclassname">xml.sax.saxutils.</code><code class="sig-name descname">XMLFilterBase</code><span class="sig-paren">(</span><em class="sig-param">base</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.sax.saxutils.XMLFilterBase" title="永久链接至目标">¶</a></dt>
<dd><p>这个类被设计用来分隔 <a class="reference internal" href="xml.sax.reader.html#xml.sax.xmlreader.XMLReader" title="xml.sax.xmlreader.XMLReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLReader</span></code></a> 和客户端应用的事件处理程序。 在默认情况下，它除了将请求传送给读取器并将事件传送给处理程序之外什么都不做，但其子类可以重载特定的方法以在传送它们的时候修改事件流或配置请求。</p>
</dd></dl>

<dl class="function">
<dt id="xml.sax.saxutils.prepare_input_source">
<code class="sig-prename descclassname">xml.sax.saxutils.</code><code class="sig-name descname">prepare_input_source</code><span class="sig-paren">(</span><em class="sig-param">source</em>, <em class="sig-param">base=''</em><span class="sig-paren">)</span><a class="headerlink" href="#xml.sax.saxutils.prepare_input_source" title="永久链接至目标">¶</a></dt>
<dd><p>此函数接受一个输入源和一个可选的基准 URL 并返回一个经过完整解析可供读取的 <a class="reference internal" href="xml.sax.reader.html#xml.sax.xmlreader.InputSource" title="xml.sax.xmlreader.InputSource"><code class="xref py py-class docutils literal notranslate"><span class="pre">InputSource</span></code></a>。 输入源的给出形式可以为字符串、文件类对象或 <a class="reference internal" href="xml.sax.reader.html#xml.sax.xmlreader.InputSource" title="xml.sax.xmlreader.InputSource"><code class="xref py py-class docutils literal notranslate"><span class="pre">InputSource</span></code></a> 对象；解析器将使用此函数来针对它们的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">parse()</span></code> 方法实现多态 <em>source</em> 参数。</p>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="xml.sax.handler.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax.handler</span></code> --- Base classes for SAX handlers</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="xml.sax.reader.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax.xmlreader</span></code> --- Interface for XML parsers</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/xml.sax.utils.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="xml.sax.reader.html" title="xml.sax.xmlreader --- Interface for XML parsers"
             >下一页</a> |</li>
        <li class="right" >
          <a href="xml.sax.handler.html" title="xml.sax.handler --- Base classes for SAX handlers"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="markup.html" >结构化标记处理工具</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>